/**
 * Copyright: Copyright (c) 2018 
 * Company:
 * 
 * @author XueWei
 * @date 2018年9月5日 上午11:44:38
 * @version V1.0
 */
package com.smartcommunity.service.dataImport;

import com.smartcommunity.base.bean.ExceptionCode;
import com.smartcommunity.base.common.PicImport;
import com.smartcommunity.dao.village.RealCompanyDao;
import com.smartcommunity.exception.BusinessException;
import com.smartcommunity.log.interceptor.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
  * @ClassName: RealCompanyPicImport
  * @Description: TODO
  * @author XueWei
  * @date 2018年9月5日 上午11:44:38
  *
  */
@Component("realCompanyPic")
@Slf4j
public class RealCompanyPicImport implements PicImport {

    @Autowired
    private RealCompanyDao realCompanyDao;

    /**
     * key taskId,value progress
     */
    final int cacheSize = 100000;
    Map<String, Integer> progressTag = new LinkedHashMap<String, Integer>((int) Math.ceil(cacheSize / 0.75f) + 1, 0.75f, true) {
        @Override
        protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
            return size() > cacheSize;
        }
    };

    @Override
    public int getImportPicProgress(String taskId) {
        return progressTag.get(taskId) == null ? 0 : progressTag.get(taskId);
    }


    /**
      * <p>Title: importPic</p>
      * <p>Description: 批量导入实有单位图片</p>
      * @param pathList
      */
    @Override
    @Log(info = "实有单位照片导入")
    public void importPic(List<Map<String, Object>> pathList,String taskId){
        try {
            progressTag.put(taskId,50);
            realCompanyDao.updateRealCompanyByList(pathList);
            progressTag.put(taskId,100);
        } catch (Exception e) {
            log.error("更新实有单位照片失败！", e);
            progressTag.put(taskId,100);
            throw new BusinessException(ExceptionCode.COMPANY_PIC_IMPORT_EXCEPTION_CODE,"实有单位图片导入失败！");
        }
        
    }
    
}
